package com.foot.travel.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.fragment.OutdoorListFragment;
import com.foot.travel.fragment.SchoolTravelListFragment;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.GetSchoolTravelTabTitles;
import com.koalac.ironman.net.pojo.response.Response;
import com.shizhefei.view.indicator.FragmentListPageAdapter;
import com.shizhefei.view.indicator.Indicator.OnTransitionListener;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.IndicatorViewPager.IndicatorFragmentPagerAdapter;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;

public class SchoolTravelListActivity extends ModuleActivity{

    private IndicatorViewPager mIndicatorViewPager;
	private LayoutInflater mInflate;
	private String[] mNames = { "班级旅游", "户外拓展"};
	private int[] mNameImages = { R.drawable.icon_list_class, R.drawable.icon_list_outdoor};
	private ScrollIndicatorView indicator;
	private SchoolTravelListFragment mSchoolTravelListFragment;
	private OutdoorListFragment mOutdoorListFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBackButtonVisibility(true);
		setNavigationTitle("班级游");
		ViewPager viewPager = (ViewPager) findViewById(R.id.moretab_viewPager);
		indicator = (ScrollIndicatorView) findViewById(R.id.moretab_indicator);
		indicator.setScrollBar(new ColorBar(this, getResources().getColor(R.color.foot_nav), 5));
		
		/**
		 * 设置滚动监听
		 */
		int selectColorId = R.color.foot_nav;
		int unSelectColorId = R.color.about_text;
		indicator.setOnTransitionListener(new OnTransitionListener() {
			
			@Override
			public void onTransition(View view, int position, float selectPercent) {
			}
		});
//		indicator.setOnTransitionListener(new OnTransitionTextListener()
//				.setColorId(this, selectColorId, unSelectColorId));
		viewPager.setOffscreenPageLimit(mNames.length);
		mIndicatorViewPager = new IndicatorViewPager(indicator, viewPager);
		mInflate = LayoutInflater.from(getApplicationContext());
		mSchoolTravelListFragment = new SchoolTravelListFragment();
			mOutdoorListFragment = new OutdoorListFragment();
		
		setTitles();
	}
	
	public void setTitles() {
	    GetSchoolTravelTabTitles getSchoolTravelTabTitles = new GetSchoolTravelTabTitles();
	    MyRequest.request(SchoolTravelListActivity.this, getSchoolTravelTabTitles, new ResponseCallback() {
	        
	        @Override
	        public void process(boolean result, Context context, Response data,
	    	    String msg) {
	            	if (result) {
	            	    String shcoolTravelName = data.getObject().optString("shcoolTravelName");
			    String outDoorName = data.getObject().optString("outDoorName");
			    mNames = new String[]{shcoolTravelName,outDoorName};
//			    mNames = new String[]{"班级","户外"};
			}
	            	 mIndicatorViewPager.setAdapter(new MyAdapter(
					getSupportFragmentManager()));
	        }
	    });
	}
	

	private class MyAdapter extends IndicatorFragmentPagerAdapter {

		public MyAdapter(FragmentManager fragmentManager) {
			super(fragmentManager);
		}

		@Override
		public int getCount() {
			return mNames.length;
		}

		@Override
		public View getViewForTab(int position, View convertView,
				ViewGroup container) {
			if (convertView == null) {
				convertView = mInflate.inflate(R.layout.school_travel_item_top,
						container, false);
			}
			TextView textView = (TextView) convertView.findViewById(R.id.text);
			ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
			textView.setText(mNames[position % mNames.length]);
			textView.setPadding(20, 0, 20, 0);
			imageView.setImageResource(mNameImages[position % mNameImages.length]);
			return convertView;
		}

		@Override
		public Fragment getFragmentForPage(int position) {
		    if (position == 0) {
			return mSchoolTravelListFragment;
		    }
		    if (position == 1) {
			return mOutdoorListFragment;
		    }
		    return null;
		}

		@Override
		public int getItemPosition(Object object) {
			return FragmentListPageAdapter.POSITION_NONE;
		}

	}

	@Override
	protected void initLayout(ViewGroup rootContentView) {
	 addContentView(R.layout.activity_school_travel);
	};
	
}
